﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TinyToolKit.FSM;

namespace TinyToolKit.Demo 
{
    public class ChaseState : FsmState<Enemy>
    {
        public float rotateSmooth = 3;
        public float chaseSpeed = 5;

        private float detectRadius;
        private Transform enemy;
        private Transform player;

        protected override void OnInit()
        {
            base.OnInit();

            enemy = FSM.GetData<Transform>(TinyDefine_Demo.FSM_EnemyTF);
            detectRadius = FSM.GetData<float>(TinyDefine_Demo.FSM_DetectedRadius);
            chaseSpeed = FSM.GetData<float>(TinyDefine_Demo.FSM_EnemySpeed);
            player = GameObject.Find("Player").transform;
        }

        protected override void OnAct()
        {
            base.OnAct();

            Vector3 forward = player.position - enemy.position;
            forward = new Vector3(forward.x, 0, forward.z);
            Quaternion targetQuaternion = Quaternion.LookRotation(forward, Vector3.up);
            enemy.rotation = Quaternion.Slerp(enemy.rotation, targetQuaternion, Time.deltaTime * rotateSmooth);
            enemy.Translate(Vector3.forward * Time.deltaTime * chaseSpeed);

            if (Vector3.Distance(enemy.position, player.position) > detectRadius + 0.5f) 
            {
                FSM.ChangeState(typeof(PatrolState));
            }
        }
    }
}
